<?php
class AddressModel extends Model{

//-----------------------------select--------------------------------------------------
	public function get_local(){
		$sql = "select * from meeting_roomlocal";
		return M('meeting_roomlocal','','DB_MEETING')->query($sql);
	}
	public function name_local($data){
		return M('meeting_roomlocal','','DB_MEETING')->where("local_name='".$data."'")->select();
	}
	public function select_local($data){
		$sql = "select * from meeting_roomlocal where local_id=".$data;
		return M('meeting_roomlocal','','DB_MEETING')->query($sql);
	}
	public function get_building($data){
		$sql = "SELECT * FROM meeting_roombuilding WHERE local_id=".$data;
		return M('meeting_roombuilding','','DB_MEETING')->query($sql);
	}
	public function get_floor($data){
		$sql = "select * from meeting_roomfloor where building_id=".$data;
		return M('meeting_roomfloor','','DB_MEETING')->query($sql);
	}
	public function get_more(){
		$sql = "SELECT meeting_roomlocal.*,meeting_roombuilding.building_name,meeting_roomfloor.* FROM `meeting_roomlocal` LEFT JOIN meeting_roombuilding on meeting_roomlocal.local_id = meeting_roombuilding.local_id LEFT JOIN meeting_roomfloor on meeting_roombuilding.building_id = meeting_roomfloor.floor_id";
		return M('meeting_roomlocal','','DB_MEETING')->query($sql);
	}

//-----------------------------insert--------------------------------------------------
	public function add_city($data)
	{
		return M('meeting_roomlocal','','DB_MEETING')->add($data);
	}

	public function add_floor($data)
	{
		$cows = '`building_id`, `floor_name`';
		$val  = "{$data['building_id']},'{$data['floor_name']}'";
		$sql  = sprintf('insert into meeting_roomfloor(%s) values(%s)',$cows,$val);
		return M('meeting_roomfloor','','DB_MEETING')->execute($sql);

	}
//-----------------------------update--------------------------------------------------
	public function update_city($id,$data){
		$sql = 'UPDATE meeting_roomlocal SET local_name="'.$data.'" WHERE local_id="'.$id.'"';
		return M('meeting_roomlocal','','DB_MEETING')->query($sql);
	}
	public function save_city($data){
		return M('meeting_roomlocal','','DB_MEETING')->save($data);
	}
	public function save_building($where,$data){
		return M('meeting_roombuilding','','DB_MEETING')->where("building_id=".$where)->save($data);
	}

	public	function save_floor($where,$data){
		return M('meeting_roomfloor','','DB_MEETING')->where("floor_id=".$where)->save($data);
	}
//-----------------------------delete--------------------------------------------------
	public function delete_location($local_id)
	{
		$this->delete_building_by_location($local_id);
		return M('meeting_roomlocal','','DB_MEETING')->delete($local_id);
	}
	public function delete_building_by_location($local_id)
	{
		$rs = $this -> get_building($local_id);
		foreach($rs as $val)
		{
			$this->delete_floor_by_building($val['building_id']);
		}
		return M('meeting_roombuilding','','DB_MEETING')->where('local_id='.$local_id)->delete();
	}
	public function delete_building($build_id)
	{
		$this->delete_floor_by_building($build_id);
		return M('meeting_roombuilding','','DB_MEETING')->where('building_id='.$build_id)->delete();
	}
	public function delete_floor_by_building($build_id)
	{
		return M('meeting_roomfloor','','DB_MEETING')->where('building_id='.$build_id)->delete();
	}
	public function delete_floor($floor_id)
	{
		return M('meeting_roomfloor','','DB_MEETING')->delete($floor_id);
	}
}


?>